package com.lkm.app_ui.m.Imple.display_second;

import android.app.Dialog;
import android.app.Presentation;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lkm.app_ui.MyApplication;
import com.lkm.app_ui.R;
import com.lkm.app_ui.log.SystemLogger;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;



public abstract class PresentationWarpFragment extends DialogFragment implements FragmentManager.OnBackStackChangedListener {



    protected abstract Fragment getFragment();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.content, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Fragment f = getChildFragmentManager().findFragmentById(R.id.content);
        if (f != null) {
        } else {
            getChildFragmentManager().beginTransaction().replace(R.id.content, getFragment()).addToBackStack(null).commit();
        }
        getChildFragmentManager().addOnBackStackChangedListener(this);
    }

    @Override
    public void onBackStackChanged() {
        if (getChildFragmentManager().getBackStackEntryCount() == 0) {
            SystemLogger.UI.onPresentationFragmentNotUI();

            dismiss();
        }

    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Display display=DisplayUtil.getDisplay(MyApplication.getInstance());
        if(display==null) {
            return super.onCreateDialog(savedInstanceState);
        }
        Dialog dialog = new Presentation(getActivity(), display);
        return dialog;
    }


}
